package cn.lingyangwl.agile.pay.common.assembly;

import cn.lingyangwl.agile.pay.model.rqrs.pay.payorder.TradeDataRS;
import cn.lingyangwl.agile.pay.model.entity.TradeData;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.Named;

import java.util.Date;
import java.util.Objects;

/**
 * @author shenguangyang
 */
@Mapper(componentModel = "spring")
public interface TradeDataAssembly {

    @Mappings({
            @Mapping(source = "createdAt", target = "createdAt", qualifiedByName = "dateToTimeMillis"),
            @Mapping(source = "updatedAt", target = "updatedAt", qualifiedByName = "dateToTimeMillis"),
    })
    TradeDataRS toTradeDataResp(TradeData tradeData);

    @Named("dateToTimeMillis")
    default Long dateToTimeMillis(Date date) {
        if (Objects.isNull(date)) {
            return System.currentTimeMillis();
        }
        return date.getTime();
    }
}
